package com.wc.AlgoOJ_train.train3_算法大礼包.c1_广度优先搜索.AG0205_流星雨;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/27 16:57
 * @description http://43.138.190.70:8888/p/AG0205
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 310, M = 50010;
    static boolean[][] isNotSafe = new boolean[N][N];
    static int[][] starTime = new int[N][N];
    static int[][] step = new int[N][N];
    static int[] posX = new int[]{-1, 1, 0, 0};
    static int[] posY = new int[]{0, 0, -1, 1};
    static int x, y, m, t;

    public static void main(String[] args) {
        m = sc.nextInt();
        while (m-- > 0) {
            x = sc.nextInt();
            y = sc.nextInt();
            t = sc.nextInt();
            if (starTime[x][y] == 0) {
                starTime[x][y] = t;
            } else {
                starTime[x][y] = Math.min(starTime[x][y], t);
            }
            isNotSafe[x][y] = true;
            // 还有上下左右的雷区
            for (int i = 0; i < 4; i++) {
                int nx = x + posX[i];
                int ny = y + posY[i];
                if (!isBorder(nx, ny)) {
                    if (starTime[nx][ny] == 0) {
                        starTime[nx][ny] = t;
                    } else {
                        starTime[nx][ny] = Math.min(starTime[nx][ny], t);
                    }
                    isNotSafe[nx][ny] = true;
                }
            }
        }

        out.println(bfs());
        out.flush();
    }

    static int bfs() {
        x = 0;
        y = 0;
        Queue<Integer> q = new LinkedList<>();
        q.add(x);
        q.add(y);
        while (!q.isEmpty()) {
            x = q.poll();
            y = q.poll();
            if (!isNotSafe[x][y]) {
                return step[x][y];
            }
            for (int i = 0; i < 4; i++) {
                int nx = x + posX[i];
                int ny = y + posY[i];
                if (!isBorder(nx, ny) &&
                        (!isNotSafe[nx][ny] || starTime[nx][ny] > step[x][y] + 1) &&
                        step[nx][ny] == 0) {
                    step[nx][ny] = step[x][y] + 1;
                    q.add(nx);
                    q.add(ny);
                }
            }
        }
        return -1;
    }

    static boolean isBorder(int x, int y) {
        // 这个是可能走到300 * 300 外面去
        return x < 0 || x > 305 || y < 0 || y > 305;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
